package com.gui.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gui.common.result.Result;
import com.gui.common.utils.MD5;
import com.gui.hosp.service.HospitalSetService;
import com.gui.model.hosp.HospitalSet;
import com.gui.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author GodLamp
 * @date 2022/10/13 12:33
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping(path = "/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Resource
    private HospitalSetService hospitalSetService;

    /**
     * 查询所有医院信息
     * @return
     */
    @ApiOperation(value = "查询所有医院信息")
    @GetMapping(path = "findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();

        return Result.ok(list);
    }

    /**
     * 逻辑删除医院
     * @param id
     * @return
     */
    @ApiOperation(value = "逻辑删除医院")
    @DeleteMapping(path = "delete/{id}")
    public Result removeHospSet(@PathVariable(name = "id") Long id){
        boolean flag = hospitalSetService.removeById(id);

        if (flag){
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 按条件查询医院信息
     * @param current
     * @param limit
     * @param hospitalSetQueryVo
     * @return
     */
    @ApiOperation(value = "按条件查询医院信息")
    @PostMapping(path = "findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable(name = "current") long current,
                                  @PathVariable(name = "limit") long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建Page对象，设置分页
        Page<HospitalSet> page = new Page<>(current, limit);

        //构建查询条件-医院名称，编号
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();

        LambdaQueryWrapper<HospitalSet> query = new LambdaQueryWrapper<>();

        if ( !StringUtils.isEmpty(hosname)) {
            query.like(HospitalSet::getHosname,hospitalSetQueryVo.getHosname());
        }

        if (!StringUtils.isEmpty(hoscode)) {
            query.eq(HospitalSet::getHoscode, hospitalSetQueryVo.getHoscode());
        }

        //分页查询
        Page<HospitalSet> resultPage = hospitalSetService.page(page, query);

        return Result.ok(resultPage);
    }

    /**
     * 保存医院信息
     * @return
     */
    @ApiOperation(value = "保存医院信息")
    @PostMapping(path = "saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态：1-可以使用，0-不可以使用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 根据id获取医院信息
     * @return
     */
    @ApiOperation(value = "根据id获取医院信息")
    @GetMapping(path = "getHospSet/{id}")
    public Result getHospSet(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        return Result.ok(hospitalSet);
    }

    /**
     * 修改医院信息
     * @return
     */
    @ApiOperation(value = "修改医院信息")
    @PostMapping(path = "updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 批量删除
     * @return
     */
    @ApiOperation(value = "批量删除")
    @DeleteMapping(path = "batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        boolean remove = hospitalSetService.removeByIds(ids);

        return Result.ok();
    }

    /**
     * 医院设置的锁定与解锁
     * @param id
     * @param status
     * @return
     */
    @PutMapping(path = "lockHospSet/{id}/{status}")
    public Result lockHospSet(@PathVariable(name = "id") Long id,
                              @PathVariable(name = "status") Integer status){
        //根据id查询医院信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (hospitalSet == null) {
            return Result.fail();
        }

        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);

        return Result.ok();
    }

    /**
     * 发送签名秘钥
     * @param id
     * @return
     */
    @ApiOperation(value = "发送签名秘钥")
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();

        //TODO 发送短信


        return Result.ok();
    }

}
